<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
      <el-form-item label="供应商名称" prop="providerName">
        <el-input
          v-model="queryParams.providerName"
          placeholder="请输入供应商名称"
          clearable
          size="small"
          style="width:180px"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人"
          clearable
          size="small"
          style="width:180px"
        />
      </el-form-item>
      <el-form-item label="供应商电话" prop="contactTel">
        <el-input
          v-model="queryParams.contactTel"
          placeholder="请输入供应商电话"
          clearable
          size="small"
          style="width:180px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="可用状态"
          clearable
          size="small"
          style="width:180px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery" >重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->

    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="del1()" >删除</el-button>
      </el-col>
    </el-row>
    <!-- 表格工具按钮结束 -->

    <!-- 数据表格开始 -->
    <el-table v-loading="loading" border :data="providerTableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商ID" align="center" width="120" prop="providerId" />
      <el-table-column label="供应商名称" width="220" prop="providerName" />
      <el-table-column label="联系人" align="center" prop="contactName" />
      <el-table-column label="联系人手机" align="center" prop="contactMobile" />
      <el-table-column label="联系人电话" align="center" prop="contactTel" />
      <el-table-column label="银行账号" width="200" align="center" prop="bankAccount" />
      <el-table-column label="供应商地址" align="center" prop="providerAddress" />
      <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="创建时间" width="180" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleAdd(scope.row)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 分页控件开始 -->
    <el-pagination
      v-show="total>0"
      :current-page="pageNum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 分页控件结束 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      center
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item el-form-item label="供应商名称" prop="providerName">
          <el-input v-model="form.providerName" placeholder="请输入供应商名称" clearable size="small" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人" clearable size="small" />
        </el-form-item>
        <el-form-item label="手机" prop="contactMobile">
          <el-input v-model="form.contactMobile" placeholder="请输入手机" clearable size="small" />
        </el-form-item>
        <el-form-item label="电话" prop="contactTel">
          <el-input v-model="form.contactTel" placeholder="请输入电话" clearable size="small" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号" clearable size="small" />
        </el-form-item>
        <el-form-item label="地址" prop="providerAddress">
          <el-input v-model="form.providerAddress" placeholder="请输入地址" clearable size="small" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              :value="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->

  </div>
</template>
<script>
	import qs from 'qs';
	import ax from 'axios';
// 引入api
export default {
  // 定义页面数据
  data() {
    return {
      // 是否启用遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 分页数据总条数
      total: 0,
      // 字典表格数据
      providerTableList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
      
        providerName: undefined,
        contactName: undefined,
        contactTel: undefined,
        status: undefined
      },
	  pageNum: 1,
	  pageSize: 5,
      multipleSelection:[],
      // 表单数据
      form: {},
      // 表单校验
      rules: {
        providerName: [
          { required: true, message: '供应商名称不能为空', trigger: 'blur' }
        ],
        contactName: [
          { required: true, message: '联系人不能为空', trigger: 'blur' }
        ],
        contactMobile: [
          { required: true, message: '手机不能为空', trigger: 'blur' },
          { pattern: /^1[3|4|5|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        bankAccount: [
          { required: true, message: '银行账号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  // 勾子
  created() {
    // 查询表格数据
    this.initProvider();
	this.initStatu();
  },
  // 方法
  methods: {
    // 查询表格数据
     initProvider() {
          this.loading = true // 打开遮罩
          this.$http.post("/stock/api/provider/provpage/"+ this.pageNum+"/"+this.pageSize, this.queryParams).then(result => {
            this.providerTableList = result.data.data.records
            this.total = result.data.data.total
            this.loading = false// 关闭遮罩
          })
        },
		handleQuery() {
          this.pageNum=1;
		  this.initProvider()
		},
		statusFormatter(row){
			let v=row.status;
			return this.formatDict2(this.statusOptions,v)
		},
		initStatu() {
			this.$http.get("stock/api/user/findByType/sys_normal_disable").then(result => {
				this.statusOptions = result.data.data
				this.statusFormatter2=result.data.data
			
			})
		},
			// 分页pageSize变化时触发
		    handleSizeChange(val) {
		      this.pageSize = val	
		      // 重新查询
		      this.initProvider()
		    },
		    // 点击上一页  下一页，跳转到哪一页面时触发
		    handleCurrentChange(val) {
		      this.pageNum = val
		      // 重新查询
		      this.initProvider()
		    },

			// 打开添加的弹出层
			handleAdd(row) {
              if (this.$refs.form != undefined) {
                this.$refs.form.clearValidate();
              }
			  this.open = true
			  this.reset()
			  this.title = '添加生产供应商信息'
              this.form=JSON.parse(JSON.stringify(row))
			},

			//添加
			handleSubmit(){
              this.$refs['form'].validate((valid) => {
                if (valid) {
				this.$http.post("/stock/api/provider/addStu/",this.form).then(result=>{
				this.$message.success(result.data.msg)
				// 关闭弹出对话框
				this.open = false;
				// 重置表单
				this.reset();
				this.title='';
				//表格回溯到第一页
				this.pageNum=1;
				// 刷新表格数据
				this.initProvider();
			})
          }
      })
	},

		// 取消
		   cancel() {
			 this.reset()
			 this.open = false
			 this.title = ''
		   },

		   // 重置表单
		   reset() {
			 this.form = {
			   providerId: undefined,
			   providerName: undefined,
			   contactName: undefined,
			   contactMobile: undefined,
			   contactTel: undefined,
			   bankAccount: undefined,
			   providerAddress: undefined,
			   status: '0'
			 }
		   },

		   //重置按钮
		   resetQuery() {
			 this.queryParams = {}
             this.pageNum=1
			 this.initProvider()
		   },

		   //删除表单中的某一行数据
		   del(row) {
		   				//根据唯一的属性去操作对应的数据	stuid
		   				let stuid = row.providerId;
		   				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
		   					confirmButtonText: '确定',
		   					cancelButtonText: '取消',
		   					type: 'warning'
		   				}).then(() => {
		   					//点击 确认
		   					ax.delete(`http://localhost:8886/stock/api/provider/` + stuid).then(r => {
                              if (r.data.t == true) {
		   							this.$message({			
		   								type: 'success',
		   								message: '删除成功!',
		   							});
		   						}
		   						this.handleQuery()
		   					})
		   
		   				}).catch(() => {
		   					this.$message({
		   						type: 'info',
		   						message: '已取消删除'
		   					});
		   				});
           },

    del1(row) {
      //根据唯一的属性去操作对应的数据	stuid
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //点击 确认
        ax.post(`http://localhost:8886/stock/api/provider/del1/` ,this.multipleSelection).then(r => {
          if (r.data.t == true) {
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          }
          this.handleQuery()
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    handleSelectionChange(val){
      this.multipleSelection=val
    }
  }
}

</script>
